#coding=utf-8
from uliweb import expose, decorators
#from sqlalchemy import *
from uliweb.orm import *

@decorators.require_login
@expose('/config/messages/list')
def config_messages_list():
    from uliweb import request
    from uliweb import settings
    from uliweb.orm import get_model
    from datetime import date
    from sqlalchemy.sql import func
    from uliweb.utils.generic import make_view_field
    
    message = get_model('message')
    data_today = []
    data_last = []
    
    read_flag = request.GET.get('read', '0')
    
    #今天的消息
    today = date.today()
    condition = None
    condition = (message.c.user == request.user.id) & condition
    condition = (func.substr(message.c.create_date,1,10) == today) & condition
    
    if read_flag == '1':
        condition = (message.c.read_flag == True) & condition
    elif read_flag == '0':
        condition = (message.c.read_flag == False) & condition
        
    for k in message.filter(condition).all():
        x = {}
        x['id'] = k.id
        x['type'] = dict(settings.PARA.MESSAGE_TYPE)[k.type]
        x['memo'] = k.memo
        x['sender'] = make_view_field(message.sender, k)
        x['read_flag'] = k.read_flag
        x['create_date'] = k.create_date.date()
        data_today.append(x)

    #以前的消息
    condition = None
    condition = (message.c.user == request.user.id) & condition
    condition = (message.c.create_date < today) & condition
    if read_flag == '1':
        condition = (message.c.read_flag == True) & condition
    elif read_flag == '0':
        condition = (message.c.read_flag == False) & condition
    
    for k in message.filter(condition).all():
        x = {}
        x['id'] = k.id
        x['type'] = dict(settings.PARA.MESSAGE_TYPE)[k.type]
        x['memo'] = k.memo
        x['sender'] = make_view_field(message.sender, k)
        x['read_flag'] = k.read_flag
        x['create_date'] = k.create_date.date()
        data_last.append(x)
    
    result = {}
    result['data_today'] = data_today
    result['data_last'] = data_last
    result['read_flag'] = read_flag
    
    return result

@decorators.require_login
@expose('/config/messages/read')
def read():
    from uliweb.orm import get_model, do_
    
    Message = get_model('message')
    ids = request.POST.getlist('read_item')
    if ids:
        ids = map(int, ids)
        if request.POST.get('submit', None):#点击了标记已读
            do_(Message.table.update(Message.c.id.in_(ids)).values(read_flag = True))
            return redirect(url_for(config_messages_list))
        else:
            Message.filter(Message.c.id.in_(ids)).remove()
            return redirect(url_for(config_messages_list))
            
    else:
        return redirect(url_for(config_messages_list))
    

@expose('/config/messages/number')    
def messages_number():
    from uliweb import request
    from uliweb.orm import get_model
    
    message = get_model('message')

    if not request.user:
        return json({'success':'fail', 'data':''})
        
    condition = None
    condition = (message.c.user == request.user.id) & condition
    condition = (message.c.read_flag == False) & condition
        
    x = message.filter(condition).count()
            
    return json({'success':'ok', 'data':x})
    